package fun.linze.wallpaperwebsite.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import fun.linze.wallpaperwebsite.dao.user.RegisterDAO;
import fun.linze.wallpaperwebsite.dao.user.UserInfoDAO;
import fun.linze.wallpaperwebsite.domain.TUser;
import fun.linze.wallpaperwebsite.service.TUserService;
import fun.linze.wallpaperwebsite.mapper.TUserMapper;
import fun.linze.wallpaperwebsite.util.JwtUtil;
import org.springframework.stereotype.Service;

/**
* @author wangy
* @description 针对表【t_user(用户表)】的数据库操作Service实现
* @createDate 2025-06-27 18:02:42
*/
@Service
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser>
    implements TUserService{

    @Override
    public boolean isRegisterValid(RegisterDAO registerDAO) {
        // 检查用户名和邮箱是否已存在
        long count = this.lambdaQuery()
                .eq(TUser::getUsername, registerDAO.getUsername())
                .or()
                .eq(TUser::getEmail, registerDAO.getEmail())
                .count();
        return count == 0;
    }

    @Override
    public boolean registerUser(RegisterDAO registerDAO) {
        //调用isRegisterValid方法检查注册信息是否合法
        if (isRegisterValid(registerDAO)) {
            // 创建新的用户对象
            TUser newUser = new TUser();
            newUser.setUsername(registerDAO.getUsername());
            newUser.setEmail(registerDAO.getEmail());
            newUser.setPassword(registerDAO.getPassword());

            // 保存用户到数据库
            return this.save(newUser);
        }
        return false;
    }

    @Override
    public UserInfoDAO getUserInfo(String username) {
        TUser tUser = this.lambdaQuery()
                .select(TUser::getId, TUser::getUsername, TUser::getEmail, TUser::getAvatarUrl, TUser::getCreatedAt, TUser::getUpdatedAt)
                .eq(TUser::getUsername, username)
                .one();
        if (tUser == null) {
            return null;
        }
        UserInfoDAO userInfoDAO = new UserInfoDAO();
        userInfoDAO.setId(tUser.getId());
        userInfoDAO.setUsername(tUser.getUsername());
        userInfoDAO.setEmail(tUser.getEmail());
        userInfoDAO.setAvatarUrl(tUser.getAvatarUrl());
        userInfoDAO.setCreatedAt(tUser.getCreatedAt());
        userInfoDAO.setUpdatedAt(tUser.getUpdatedAt());
        return userInfoDAO;
    }

    @Override
    public String login(String username, String password) {
        //检查用户名和密码是否匹配
        TUser user = this.lambdaQuery()
                .eq(TUser::getUsername, username)
                .eq(TUser::getPassword, password)
                .one();
        if (user == null) {
            return null; // 用户名或密码错误
        }

        // 生成 JWT Token
        JwtUtil jwtUtil = new JwtUtil();
        String token = jwtUtil.generateToken(user.getUsername());


        return token; // 返回生成的 Token
    }

    @Override
    public boolean updateUserInfo(String username, UserInfoDAO userInfoDAO) {
        // 根据用户名查询用户
        TUser user = this.lambdaQuery()
                .eq(TUser::getUsername, username)
                .one();
        //仅可更新昵称
    if (user != null) {
        // 更新用户信息
        user.setNickname(userInfoDAO.getNickname());
        user.setAvatarUrl(userInfoDAO.getAvatarUrl());
        user.setUpdatedAt(userInfoDAO.getUpdatedAt());

        // 保存更新后的用户信息
        return this.updateById(user);
    }
        return false;
    }

    public String getUsernameFromToken(String token) {
        // 使用 JwtUtil 获取用户名
        System .out.println("获取用户名的令牌: " + token);
        return JwtUtil.getUsernameFromToken(token);
    }
}




